<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author lzw
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'operating:ftdsAzj:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm">
      <template #ftdsAzjhkTableList>
        <BasicTable
          @register="registerFtdsAzjhkTableTable"
          @row-click="handleFtdsAzjhkTableRowClick"
        />
        <a-button class="mt-2" @click="handleFtdsAzjhkTableAdd" v-auth="'operating:ftdsAzj:edit'">
          <Icon icon="i-ant-design:plus-circle-outlined" /> {{ t('新增') }}
        </a-button>
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsOperatingFtdsAzjForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicTable, useTable } from '/@/components/Table';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { FtdsAzj, ftdsAzjSave, ftdsAzjForm } from '/@/api/operating/ftdsAzj';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('operating.ftdsAzj');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<FtdsAzj>({} as FtdsAzj);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增爱租机数据管理') : t('编辑爱租机数据管理'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('商户名称'),
      field: 'azjShmc',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('订单号'),
      field: 'azjDdh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('债务人姓名'),
      field: 'azjZwrxm',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('债务人联系电话'),
      field: 'azjZwrlxdh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('债务人身份证号'),
      field: 'azjZwrsfzh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('债务人身份证地址'),
      field: 'azjZwrsfzdz',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('债务人民族'),
      field: 'azjZwrmz',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('债务人通讯地址'),
      field: 'azjZwrtxdz',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('订单签约价'),
      field: 'azjDdqyj',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('订单买断价'),
      field: 'azjDdmdj',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('分期期数'),
      field: 'azjFqqs',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('月租金'),
      field: 'azjYzj',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('逾期期数'),
      field: 'azjYqqs',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('逾期本金'),
      field: 'azjYqbj',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('滞纳金'),
      field: 'azjZnj',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('剩余应还'),
      field: 'azjSyyh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('租用日期'),
      field: 'azjZyrq',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('手机型号'),
      field: 'azjSjxh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('发货日期'),
      field: 'azjFhrq',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('物流单号'),
      field: 'azjWldh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('签收日期'),
      field: 'azjQsrq',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('逾期日期'),
      field: 'azjYqrq',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('紧急联系人'),
      field: 'azjJjlxr',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('芝麻收货手机号'),
      field: 'azjZmshsjh',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('已交租'),
      field: 'azjYjz',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('IMEI/SN'),
      field: 'azjImeisn',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('首期券后签约价值'),
      field: 'azjSqqhqyjz',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('押金'),
      field: 'azjYj',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('累计期数'),
      field: 'azjLjqs',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('爱租机还款计划表'),
      field: 'ftdsAzjhkTableList',
      component: 'Input',
      colProps: { lg: 24, md: 24 },
      slot: 'ftdsAzjhkTableList',
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerFtdsAzjhkTableTable, ftdsAzjhkTableTable] = useTable({
    actionColumn: {
      width: 60,
      actions: (record: Recordable) => [
        {
          icon: 'i-ant-design:delete-outlined',
          color: 'error',
          popConfirm: {
            title: '是否确认删除',
            confirm: handleFtdsAzjhkTableDelete.bind(this, record),
          },
          auth: 'operating:ftdsAzj:edit',
        },
      ],
    },
    rowKey: 'id',
    pagination: false,
    bordered: true,
    size: 'small',
    inset: true,
  });

  async function setFtdsAzjhkTableTableData(_res: Recordable) {
    ftdsAzjhkTableTable.setColumns([
      {
        title: t('期数'),
        dataIndex: 'tableQs',
        width: 130,
        align: 'left',
        editRow: true,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 100,
        },
        editRule: false,
      },
      {
        title: t('应还租日'),
        dataIndex: 'tableYhzr',
        width: 130,
        align: 'left',
        editRow: true,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 100,
        },
        editRule: false,
      },
      {
        title: t('实还租金'),
        dataIndex: 'tableShzj',
        width: 130,
        align: 'left',
        editRow: true,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 100,
        },
        editRule: false,
      },
      {
        title: t('未还租金'),
        dataIndex: 'tableWhzj',
        width: 130,
        align: 'left',
        editRow: true,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 100,
        },
        editRule: false,
      },
      {
        title: t('本期是否结清'),
        dataIndex: 'tableBqsfjq',
        width: 130,
        align: 'left',
        editRow: true,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 100,
        },
        editRule: false,
      },
      {
        title: t('本期结清方式'),
        dataIndex: 'tableBqjqfs',
        width: 130,
        align: 'left',
        editRow: true,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 100,
        },
        editRule: false,
      },
      {
        title: t('本期结清时间'),
        dataIndex: 'tableBqjqsj',
        width: 130,
        align: 'left',
        editRow: true,
        editComponent: 'Input',
        editComponentProps: {
          maxlength: 100,
        },
        editRule: false,
      },
    ]);
    ftdsAzjhkTableTable.setTableData(record.value.ftdsAzjhkTableList || []);
  }

  function handleFtdsAzjhkTableRowClick(record: Recordable) {
    record.onEdit?.(true, false);
  }

  function handleFtdsAzjhkTableAdd() {
    ftdsAzjhkTableTable.insertTableDataRecord({
      id: new Date().getTime(),
      isNewRecord: true,
      editable: true,
    });
  }

  function handleFtdsAzjhkTableDelete(record: Recordable) {
    ftdsAzjhkTableTable.deleteTableDataRecord(record);
  }

  async function getFtdsAzjhkTableList() {
    let ftdsAzjhkTableListValid = true;
    let ftdsAzjhkTableList: Recordable[] = [];
    for (const record of ftdsAzjhkTableTable.getDataSource()) {
      if (!(await record.onEdit?.(false, true))) {
        ftdsAzjhkTableListValid = false;
      }
      ftdsAzjhkTableList.push({
        ...record,
        id: !!record.isNewRecord ? '' : record.id,
      });
    }
    for (const record of ftdsAzjhkTableTable.getDelDataSource()) {
      if (!!record.isNewRecord) continue;
      ftdsAzjhkTableList.push({
        ...record,
        status: '1',
      });
    }
    if (!ftdsAzjhkTableListValid) {
      throw { errorFields: [{ name: ['ftdsAzjhkTableList'] }] };
    }
    return ftdsAzjhkTableList;
  }

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await ftdsAzjForm(data);
    record.value = (res.ftdsAzj || {}) as FtdsAzj;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setFtdsAzjhkTableTableData(res);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      data.ftdsAzjhkTableList = await getFtdsAzjhkTableList();
      // console.log('submit', params, data, record);
      const res = await ftdsAzjSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
